package com.conncui.common.json.impl;

import com.conncui.common.json.dao.Json;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

/**
 * Created by cui on 16-3-1.
 */
public class GsonImpl implements Json {
    Gson gson;

    public GsonImpl() {
        gson = new Gson();
    }

    @Override
    public String objectToString(Object object) {
        String json = gson.toJson(object);
        return json;
    }

    @Override
    public <T> T stringToObject(String jsonString, Class<T> tClass) {
        T t = gson.fromJson(jsonString, tClass);
        return t;
    }

    @Override
    public <T> List<T> stringToObjectList(String jsonString, Class<T> tClass) {
        List<T> tList = gson.fromJson(jsonString, new TypeToken<List<T>>() {}.getType());
        return tList;
    }

    @Override
    public String objectListToString(Object objectList) {
        String json = gson.toJson(objectList);
        return json;
    }
}
